package edu.uts.reekie.designer.actions;

import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;

import edu.uts.reekie.designer.edit.AbstractConnectorEndpointEditPolicy;
import edu.uts.reekie.designer.model.AbstractConnector;

public class SetConnectorLabelAction extends ConnectorLabelAction {

	public static final String NORMAL = "Set Connector Label";
	
	public SetConnectorLabelAction(IWorkbenchPart part) {
		super(part);
	}

	
	protected boolean canPerformAction(AbstractConnector connector) {
		return true;
	}
	
	protected String generateId() {
		return NORMAL;
	}
	
	
	public void run() {
		
		// reset the request
		request.getExtendedData().clear();
		
		// open the dialog for changed value
		int style = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().getStyle();
		Shell shell = new Shell((style & SWT.MIRRORED) != 0 ? SWT.RIGHT_TO_LEFT : SWT.NONE);
		SetConnectorLabelDialog dialog = new SetConnectorLabelDialog(shell,generateId());
		int result = dialog.open();
		
		if (result == SetConnectorLabelDialog.OK) {
			String val = dialog.getEnteredLabel();
			
			request.getExtendedData().put(AbstractConnectorEndpointEditPolicy.NEW_LABEL, val);
			// if all good, run the command
			super.run();
		}
	}
	
	protected  Command getCommand() {
		List<?> editparts = getSelectedObjects();
		CompoundCommand cc = new CompoundCommand();
		cc.setDebugLabel("Set Connector Label");//$NON-NLS-1$
		for (int i=0; i < editparts.size(); i++) {
			EditPart part = (EditPart)editparts.get(i);
			cc.add(part.getCommand(request));
		}
		return cc;
	}
	
}
